package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.Role;
import com.example.mall.dto.RoleDto;
import com.example.mall.mapper.RoleMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.RoleQuery;
import com.example.mall.service.IRoleService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import com.example.mall.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-04-30
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageDTO<RoleDto> queryPage(RoleQuery roleQuery) {
        if (roleQuery == null) {
            roleQuery = new RoleQuery();
        }
        Page<Role> page = roleQuery.toMpPageByDefaultCreateTimeAsc();
        Page<Role> cartPage = lambdaQuery()
                .like(roleQuery.getRoleId()!= null, Role::getRoleId, roleQuery.getRoleId())
                .like(StringUtils.isNotEmpty(roleQuery.getRoleName()),Role::getRoleName,roleQuery.getRoleName())
                .like(StringUtils.isNotEmpty(roleQuery.getRoleDescription()),Role::getRoleDescription,roleQuery.getRoleDescription())
                .like(StringUtils.isNotEmpty(roleQuery.getRoleState()),Role::getRoleState,roleQuery.getRoleState())
                .orderByDesc(Role::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, RoleDto.class);
    }

    @Override
    public Role queryRoleById(Long roleId) {
        return roleMapper.selectById(roleId);
    }

    @Override
    public List<Role> queryRoleList() {
        return this.list();
    }

    @Override
    public void insert(RoleDto roleDto) {
        Role role = new Role();
        CopyUtils.copyProperties(roleDto, role);
        role.setRoleId(CodeGenerateUtils.generateUniqueId());
        role.setCreateDate(new Date());
        role.setUpdateDate(new Date());
        roleMapper.insert(role);
    }

    @Override
    public void update(RoleDto roleDto) {
        AssertUtil.isNotNull(roleDto.getRoleId(), "角色ID不能为空");
        Role role = roleMapper.selectById(roleDto.getRoleId());
        AssertUtil.isNotNull(role, "角色不存在");
        CopyUtils.copyProperties(roleDto, role);
        role.setUpdateDate(new Date());
        roleMapper.updateById(role);
    }

    @Override
    public void delete(Long roleId) {
        AssertUtil.isNotNull(roleId, "角色ID不能为空");
        Role role = roleMapper.selectById(roleId);
        AssertUtil.isNotNull(role, "角色不存在");
        role.setRoleState(String.valueOf(BaseConstants.LOSE_STATUS));
        role.setUpdateDate(new Date());
        roleMapper.updateById(role);
    }

    @Override
    public void deleteBatch(List<Long> roleIds) {
        AssertUtil.isNotNull(roleIds, "角色ID列表不能为空");
        List<Role> roles = roleMapper.selectBatchIds(roleIds);
        AssertUtil.isNotNull(roles, "角色不存在");
        roles.stream().forEach(item->{
            item.setRoleState(String.valueOf(BaseConstants.LOSE_STATUS));
            item.setUpdateDate(new Date());
            roleMapper.updateById(item);
        });
    }
}
